﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThermalEquipmentDesign.Model.CasingPipeWing
{
    public class CasingPipeWingModel
    {
        public CasingPipeWingResult result = new CasingPipeWingResult();
        public CasingPipeWing casingpipewing;

        /// <summary>
        /// Calculate Number Tubes In Way
        /// </summary>
        /// <returns>Number Tubes In Way</returns>
        private double NumberTubesInWay()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.PI * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232) + 1;
            return Math.Round(n1,0);
        }

        /// <summary>
        /// Calculate Water Speed
        /// </summary>
        /// <returns>Water Speed</returns>
        private double WaterSpeed()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232) + 1;
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            return Math.Round(w,5);
        }

        /// <summary>
        /// Calculate Reynolds Value
        /// </summary>
        /// <returns>Reynolds Value</returns>
        private double ReynoldsValue()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232) + 1;
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            return Math.Round(Re,0);
        }

        /// <summary>
        /// Calculate Nusselt Value
        /// </summary>
        /// <returns>Nusselt Value</returns>
        private double NusseltValue()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            return Math.Round(Nu,2);
        }

        /// <summary>
        /// Calculate Heat Transfer Coefficient
        /// </summary>
        /// <returns>Heat Transfer Coefficient</returns>
        private double HeatTransferCoefficient()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            return Math.Round(abi, 0);
        }

        /// <summary>
        /// Calculate Density Water Salt
        /// </summary>
        /// <returns>Density Water Salt</returns>
        private double DensityWaterSalt()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            return Math.Round(qFb,1);
        }

        /// <summary>
        /// Calculate Density Heat R22
        /// </summary>
        /// <returns>Density Heat R22</returns>
        private double DensityHeatR22()
        {
            double qFR22 = 568 * Math.Pow(2.964, 0.45) * casingpipewing.DbOuterDiameter / casingpipewing.DbDiameter;
            return Math.Round(qFR22, 0);
        }

        /// <summary>
        /// Calculate Heat Exchange Area
        /// </summary>
        /// <returns>Heat Exchange Area</returns>
        private double HeatExchangeArea()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            return Math.Round(Fi,2);
        }

        /// <summary>
        /// Calculate Buoc Ong
        /// </summary>
        /// <returns>Segment Tube</returns>
        private double BuocOng()
        {
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            return BO;
        }

        /// <summary>
        /// Calculate Tubes Layout
        /// </summary>
        /// <returns>Tubes Layout</returns>
        private double TubesLayout()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            double nc = 0.75 * Math.Pow(Fi / (casingpipewing.DbDiameter * 6 * BO), 1 / 3);
            return nc;
        }

        /// <summary>
        /// Calculate Inside Diameter
        /// </summary>
        /// <returns>Inside Diameter</returns>
        private double InsideDiameter()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            double nc = 0.75 * Math.Pow(Fi / (casingpipewing.DbDiameter * 6 * BO), 1 / 3);
            double Di = nc * BO;
            return Di;
        }

        /// <summary>
        /// Calculate Length Casing
        /// </summary>
        /// <returns>Length Casing</returns>
        private double LengthCasing()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            double nc = 0.75 * Math.Pow(Fi / (casingpipewing.DbDiameter * 6 * BO), 1 / 3);
            double Di = nc * BO;
            double l = 6 * Di;
            return l;
        }

        /// <summary>
        /// Calculate Ways
        /// </summary>
        /// <returns>Ways</returns>
        private double Ways()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            double nc = 0.75 * Math.Pow(Fi / (casingpipewing.DbDiameter * 6 * BO), 1 / 3);
            double Di = nc * BO;
            double l = 6 * Di;
            double z = Fi * (Math.PI * casingpipewing.DbDiameter * l * n1);
            return z;
        }

        /// <summary>
        /// Calculate Total Tube
        /// </summary>
        /// <returns>Total Tube</returns>
        private double TotalTube()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            double nc = 0.75 * Math.Pow(Fi / (casingpipewing.DbDiameter * 6 * BO), 1 / 3);
            double Di = nc * BO;
            double l = 6 * Di;
            double z = Fi * (Math.PI * casingpipewing.DbDiameter * l * n1);
            double nt = z * n1;
            return nt;
        }

        /// <summary>
        /// Calculate Real Heat Exchange Area
        /// </summary>
        /// <returns>Real Heat Exchange Area</returns>
        private double RealHeatExchangeArea()
        {
            double n1 = 4 * casingpipewing.DbCapacity / (1.5 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * casingpipewing.DbCapacity / (n1 * Math.Pow(casingpipewing.DbDiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * casingpipewing.DbDiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / casingpipewing.DbDiameter;
            double qFb = casingpipewing.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0005);
            double Fi = casingpipewing.DbCapacity / qFb;
            double BO = 1.3 * casingpipewing.DbOuterDiameter;
            double nc = 0.75 * Math.Pow(Fi / (casingpipewing.DbDiameter * 6 * BO), 1 / 3);
            double Di = nc * BO;
            double l = 6 * Di;
            double z = Fi * (Math.PI * casingpipewing.DbDiameter * l * n1);
            double nt = z * n1;
            double RealFi = nt * l * Math.PI * casingpipewing.DbDiameter;
            return Math.Round(RealFi,4);
        }

        /// <summary>
        /// Calculate Casing Pipe Wing
        /// </summary>
        /// /// <param name="con">CasingPipeWing object</param>
        /// <returns>CasingPipeWingResult object</returns> 
        public CasingPipeWingResult ImplementCalculation(CasingPipeWing cpw)
        {
            casingpipewing = cpw;
            result.DbBuocOng = BuocOng();
            result.DbDensityHeatR22 = DensityHeatR22();
            result.DbDensityWaterSalt = DensityWaterSalt();
            result.DbHeatExchangeArea = HeatExchangeArea();
            result.DbHeatTransferCoefficient = HeatTransferCoefficient();
            result.DbInsideDiameter = InsideDiameter();
            result.DbLengthCasing = LengthCasing();
            result.DbNumberTubesInWay = NumberTubesInWay();
            result.DbNusseltValue = NusseltValue();
            result.DbReynoldsValue = ReynoldsValue();
            result.DbTotalTube = TotalTube();
            result.DbTubesLayout = TubesLayout();
            result.DbWaterSpeed = WaterSpeed();
            result.DbWays = Ways();
            result.DbRealHeatExchangeArea = RealHeatExchangeArea();
            return result;
        }
    }
}
